package com.freshcity.component.guide
{
	import com.freshcity.lib.util.ObjectPool;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.PopupManager;
	import com.freshcity.manager.support.MaskInfo;
	import com.pblabs.core.IPBManager;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.*;

	public class GuideTipsManager implements IPBManager
	{
		public static const Dir_BL:int=1;

		public static const Dir_BR:int=2;

		public static const Dir_TL:int=3;

		public static const Dir_TR:int=4;
		
		private static var _lineBoxTips:LineBoxTips;
		
		private static var _callBack:Function;
		
		private static var _daily:int;
		
		private static var _thumbParent:Sprite;		

		private var _guideTips:GuideTips;

		[Inject]
		public var layerManager:LayerManager;

		public function destroy():void
		{
			// TODO Auto Generated method stub
		}

		public function initialize():void
		{
			_guideTips=new GuideTips(layerManager);
			_lineBoxTips = new LineBoxTips();
			_thumbParent=LayerManager.guideLayer;
		}

		public function get guideTips():GuideTips
		{
			return _guideTips;
		}


		public function getGuideTips(content:String, callBack:Function=null, timeout:int=0, para:Array=null, isCenter:Boolean=true):GuideTips
		{
			_guideTips.clear();
			_guideTips.updateCallBack(callBack,timeout,para,isCenter);
			_guideTips.updateTxt(content);
			_callBack = callBack;
			_daily = timeout;
			return _guideTips;
		}
		
		public function showGuideTips(content:String,position:Point,size:Point,callBack:Function=null,timeOut:int=0,mask:Boolean=true,lineBox:Boolean=true,arrowGuide:Boolean=false,dir:int=5):void
		{
			var loc1:*=ObjectPool.getObject(MaskInfo);
			loc1.x = position.x;
			loc1.y = position.y;
			
			loc1.width = size.x;
			loc1.height = size.y;
			
			if(mask){
				EffectManager.addUIMask([loc1],0);	
			}else{
				EffectManager.addUIMask([],0);	
			}
			
			if(arrowGuide){
				var loc2:DisplayObject = GuideArrowManager.getGuideArrow(dir);
				if(dir== GuideArrowManager.Dir_L){
					loc2.x = loc1.x + loc1.width;
				}else{
					loc2.x = loc1.x;
				}
				loc2.y = loc1.y + loc1.height / 2;
				LayerManager.guideLayer.addChild(loc2);
			}else{
				GuideArrowManager.hideGuideArrow();
			}
			
			getGuideTips(content,callBack,timeOut);			
		}

		public function showGuideTipsAtLayer(label:String, fun:Function=null, x:int=0, y:int=0, type:int=1, parent:DisplayObjectContainer=null):void
		{
			var loc1:DisplayObject=getGuideTips(label, fun, type);
			if (parent == null)
			{
				parent=LayerManager.msgTipLayer;
			}

			if (x == 0 && y == 0)
			{
				loc1.x=100;
				loc1.y=500;
			}
			else
			{
				loc1.x=x;
				loc1.y=y;
			}
			parent.addChild(loc1);
		}

		private static function onDailyTimeOut():void
		{
			onTipsClick();
			_lineBoxTips.dispose();
		}

		public function hideGuideTips():void
		{
			_guideTips.destroy();
		}
		
		public static function getLineBoxTips(content:String, mask:MaskInfo=null, callback:Function=null, timeout:int=0,dir:int=1):LineBoxTips
		{
			hideLineBoxTips();
			_daily = timeout;
			_callBack = callback;
			var lwidth:int = -1;
			var lheight:int = -1;
			if(mask){
				lwidth = mask.width;
				lheight = mask.height;
				_lineBoxTips.x = mask.x;
				_lineBoxTips.y = mask.y;
				LayerManager.guideLayer.addChild(_lineBoxTips);
				_lineBoxTips.updateDir(dir);
				_lineBoxTips.updateTxt(content);
				_lineBoxTips.updateRect(mask);
				if (_callBack != null) 
				{
					_lineBoxTips.updateCallBack(_callBack);
				}
			}
			
			if (_daily != 0) 
			{
				flash.utils.setTimeout(onDailyTimeOut, _daily);
			}
			return _lineBoxTips;
		}
		
		private static function onTipsClick():void
		{
			if (_callBack != null) 
			{
				_callBack();
			}
		}		
		
		public static function showLineBoxTipsAtLayer(arg1:DisplayObjectContainer, x:int, y:int, content:String):void
		{
			var loc1:LineBoxTips = getLineBoxTips(content);
			loc1.x = x;
			loc1.y = y;
			arg1.addChild(loc1);
		}
		
		public static function hideLineBoxTips():void
		{
			_lineBoxTips.dispose();
		}		
	}
}
